<?php

Yii::import('common.models.db.ShopProductsModel');

class FrontendShopProductsModel extends ShopProductsModel
{
	public static function model($className=__CLASS__) {
		return parent::model($className);
	}
	public static function getDetail($productId)
	{
		$sql = "SELECT * FROM shop_products WHERE published=1 AND product_id=:pid";
		$command = Yii::app()->db->createCommand($sql);
		$command->bindParam(':pid', $productId);
		return $command->queryRow();
	}
	public function getProductReleat($catId, $productId, $limit=30, $offset=0, $order="")
	{
		$criteria = new CDbCriteria;
		$criteria->condition = "category_id=$catId AND product_id<>:pid AND published=1";
		$criteria->params = array(':pid'=>$productId);
		$criteria->order = "product_id DESC";
		$criteria->offset = $offset;
		$criteria->limit = $limit;
		return self::model()->findAll($criteria);
	}
	public static function getProductByCat($catId, $limit=30, $offset=0, $order="")
	{
		$where = ($catId==null)?"":" AND c2.category_id= ".intval($catId);
		$order = ($order!='')?$order:" ORDER BY c1.product_id DESC ";
		$sql = "SELECT c1.* 
				FROM shop_products c1
				INNER JOIN shop_product_category c2 On c1.product_id = c2.product_id
				WHERE c1.published=1 $where $order LIMIT :offset, :limit ";
		$command = Yii::app()->db->createCommand($sql);
		$command->bindParam(':offset', $offset);
		$command->bindParam(':limit', $limit);
		return $command->queryAll();
	}
	public static function getTotalProductByCat($catId)
	{
		$where = ($catId==null)?"":" AND c2.category_id= ".intval($catId);
		$sql = "SELECT count(*) as total
				FROM shop_products c1
				INNER JOIN shop_product_category c2 On c1.product_id = c2.product_id
				WHERE c1.published=1 $where ";
		
		$command = Yii::app()->db->createCommand($sql);
		$result = $command->queryRow();
		return $result['total'];
	}
	public static function getTotalProductFeature()
	{
		$where = "AND featured=1";
		$sql = "SELECT count(*) as total FROM shop_products WHERE published=1 $where ";
		$command = Yii::app()->db->createCommand($sql);
		$result = $command->queryRow();
		return $result['total'];
	}
	
	public function getTaxRate($variations = null, $amount = 1) {
		if($this->tax) {
			$taxrate = $this->tax->percent;
			$price = (float) $this->price;
			if($variations)
				foreach($variations as $key => $variation) {
				$price += @ProductVariation::model()->findByPk($variation[0])->price_adjustion;
			}
	
	
			(float) $price *= $amount;
	
			(float) $tax = $price * ($taxrate / 100);
	
			return $tax;
		}
	}
	public function getPrice($variations = null, $amount = 1) {
		$price = (float) $this->price;
		if($variations)
			foreach($variations as $key => $variation) {
			$price += @ProductVariation::model()->findByPk($variation[0])->price_adjustion;
		}
	
	
		(float) $price *= $amount;
	
		return $price;
	}
	
	public function updateViewd($id)
	{
		$product = self::model()->findByPk($id);
		$product->view = $product->view+1;
		return $product->save();
	}
}